package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.dao.ApplyapprovalMapper;
import com.example.demo.nuoqi.dao.ItemdetailsMapper;
import com.example.demo.nuoqi.dao.MyapplicationMapper;
import com.example.demo.nuoqi.pojo.*;
import com.example.demo.nuoqi.service.ApplyapprovalService;
import com.example.demo.nuoqi.service.ItemdetailsService;
import com.example.demo.nuoqi.service.MyapplicationService;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "MyapplicationController")
@RestController
@RequestMapping("/myapplication")
@CrossOrigin
public class MyapplicationController {

    @Autowired
    private MyapplicationService myapplicationService;
    @Autowired
    private ItemdetailsMapper itemdetailsMapper;
    @Autowired
    private ItemdetailsService itemdetailsService;
    @Autowired
    ApplyapprovalMapper applyapprovalMapper;
    @Autowired
    ApplyapprovalService applyapprovalService;
    @Autowired
    private MyapplicationMapper myapplicationMapper;

    /***
     * Myapplication分页条件搜索实现
     * @param myapplication
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Myapplication条件分页查询",notes = "分页条件查询Myapplication方法详情",tags = {"MyapplicationController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Myapplication对象",value = "传入JSON数据",required = false) Myapplication myapplication, @PathVariable int page, @PathVariable int size){
        //调用MyapplicationService实现分页条件查询Myapplication
        PageInfo<Myapplication> pageInfo = myapplicationService.findPage(myapplication, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Myapplication分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Myapplication分页查询",notes = "分页查询Myapplication方法详情",tags = {"MyapplicationController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用MyapplicationService实现分页查询Myapplication
        PageInfo<Myapplication> pageInfo = myapplicationService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param myapplication
     * @return
     */
    @ApiOperation(value = "Myapplication条件查询",notes = "条件查询Myapplication方法详情",tags = {"MyapplicationController"})
    @PostMapping(value = "/search" )
    public Result<List<Myapplication>> findList(@RequestBody(required = false) @ApiParam(name = "Myapplication对象",value = "传入JSON数据",required = false) Myapplication myapplication){
        //调用MyapplicationService实现条件查询Myapplication
        List<Myapplication> list = myapplicationService.findList(myapplication);
        return new Result<List<Myapplication>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Myapplication根据ID删除",notes = "根据ID删除Myapplication方法详情",tags = {"MyapplicationController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用MyapplicationService实现根据主键删除
        myapplicationService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Myapplication数据
     * @param myapplication
     * @param id
     * @return
     */
    @ApiOperation(value = "Myapplication根据ID修改",notes = "根据ID修改Myapplication方法详情",tags = {"MyapplicationController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Myapplication对象",value = "传入JSON数据",required = false) Myapplication myapplication, @PathVariable Integer id){
        //设置主键值
        myapplication.setId(id);
        //调用MyapplicationService实现修改Myapplication
        myapplicationService.update(myapplication);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Myapplication数据
     * @param myapplication
     * @return
     */
    @ApiOperation(value = "Myapplication添加",notes = "添加Myapplication方法详情",tags = {"MyapplicationController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Myapplication对象",value = "传入JSON数据",required = true) Myapplication myapplication){
        //调用MyapplicationService实现添加Myapplication
        myapplicationService.add(myapplication);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Myapplication数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Myapplication根据ID查询",notes = "根据ID查询Myapplication方法详情",tags = {"MyapplicationController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Myapplication> findById(@PathVariable Integer id){
        //调用MyapplicationService实现根据主键查询Myapplication
        Myapplication myapplication = myapplicationService.findById(id);
        return new Result<Myapplication>(true,StatusCode.OK,"查询成功",myapplication);
    }

    /***
     * 查询Myapplication全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Myapplication",notes = "查询所Myapplication有方法详情",tags = {"MyapplicationController"})
    @GetMapping
    public Result<List<Myapplication>> findAll(){
        //调用MyapplicationService实现查询所有Myapplication
        List<Myapplication> list = myapplicationService.findAll();
        return new Result<List<Myapplication>>(true, StatusCode.OK,"查询成功",list) ;
    }


    @ApiOperation(value = "申请加入项目",notes = "申请加入项目",tags = {"MyapplicationController"})
    @GetMapping("/join/{itemId}")
    public Result<List<Myapplication>> applicationJoinItem(@PathVariable Integer itemId, HttpSession session){
        //获取当前点击项目id 根据项目id 获取项目详细信息
        Itemdetails itemdetails = itemdetailsService.findById(itemId);
        //获取当前登录用户 往我的审批中加入数据
        User user = (User) session.getAttribute("user");
        Myapplication myapplication = new Myapplication();
        myapplication.setCreateTime(new Date());
        myapplication.setCreateName(itemdetails.getCreatePerson());
        myapplication.setItemId(itemdetails.getId());
        myapplication.setItemName(itemdetails.getItemName());
        myapplication.setApplicationPersonId(user.getId());
        myapplication.setStatus("1");
        myapplicationMapper.insertSelective(myapplication);
        int insert = myapplication.getId();

        //向审批表加入申请人数据
        Applyapproval applyapproval = new Applyapproval();
        applyapproval.setItemName(itemdetails.getItemName());
        applyapproval.setItemId(itemdetails.getId());
        applyapproval.setApplicationName(user.getName());
        applyapproval.setApplicationId(insert);
        applyapproval.setCreateTime(new Date());
        applyapproval.setStatus("1");
        applyapproval.setApprovalPersonId(user.getId());
        applyapproval.setOwnUserId(itemdetails.getUserId());
        applyapprovalService.add(applyapproval);

        return new Result(true, StatusCode.OK,"申请成功等待审批",null) ;
    }



    @ApiOperation(value = "取消加入项目",notes = "取消加入项目",tags = {"MyapplicationController"})
    @GetMapping("/cancel/{cancelApplicationJoinItemId}/{result}")
    public Result<List<Myapplication>> cancelApplicationJoinItem(@PathVariable("cancelApplicationJoinItemId") Integer cancelApplicationJoinItemId, @PathVariable("result")String result, HttpSession session){
        Myapplication myapplication = myapplicationService.findById(cancelApplicationJoinItemId);
        myapplication.setStatus("4");
        myapplicationService.update(myapplication);
        return new Result(true, StatusCode.OK,"取消加入项目成功",null) ;
    }



    @ApiOperation(value = "申请退出项目",notes = "申请退出项目",tags = {"MyapplicationController"})
    @GetMapping("/requestLeave/{requestLeaveApplicationJoinItemId}/{result}")
    public Result<List<Myapplication>> requestLeave(@PathVariable("requestLeaveApplicationJoinItemId") Integer cancelApplicationJoinItemId,@PathVariable("result")String result, HttpSession session){
        Myapplication myapplication = myapplicationService.findById(cancelApplicationJoinItemId);

        Applyapproval application = new Applyapproval();
        application.setApplicationId(myapplication.getId());
        application.setCreateTime(myapplication.getCreateTime());
        Applyapproval applyapproval = applyapprovalService.get(application);

        myapplication.setStatus(result);
        applyapproval.setStatus(result);
        myapplicationService.update(myapplication);
        applyapprovalService.update(applyapproval);

        return new Result(true, StatusCode.OK,"取消加入项目成功",null) ;
    }




    @ApiOperation(value = "获取加入项目列表",notes = "获取加入项目列表",tags = {"MyapplicationController"})
    @GetMapping("/getApplicationJoinItemList")
    public Result<List<Myapplication>> getApplicationJoinItemList(HttpSession session){
        //获取当前登录用户
        User user = UserUtils.getUser(session);
        List<Myapplication> all = myapplicationService.findAll();
        ArrayList<Myapplication> list = new ArrayList<>();
        for (Myapplication myapplication : all) {
            if (user.getId() == myapplication.getApplicationPersonId()) {
                list.add(myapplication);
            }
        }
        return new Result(true, StatusCode.OK,"获取申请加入项目列表成功",list) ;
    }


    @ApiOperation(value = "获取我参与项目列表",notes = "获取我参与项目列表",tags = {"MyapplicationController"})
    @GetMapping("/getApplicationParticipateItemList")
    public Result<List<Myapplication>> getApplicationParticipateItemList(HttpSession session){
        //获取当前登录用户
        User user = UserUtils.getUser(session);
        List<Myapplication> all = myapplicationService.findAll();
        ArrayList<Myapplication> list = new ArrayList<>();
        for (Myapplication myapplication : all) {
            if ("2".equals(myapplication.getStatus())  && user.getId() == myapplication.getApplicationPersonId()) {
                list.add(myapplication);
            }
        }
        return new Result(true, StatusCode.OK,"获取我参与项目列表",list) ;
    }


}
